package com.veeder.datacollection.rs232;

import com.veeder.datacollection.rs232.response.HeightVolumePair;
import com.veeder.datacollection.rs232.response.RS232Response;
import com.veeder.datacollection.rs232.response.TankChart;
import com.veeder.datacollection.rs232.response.TankProfileAndFullVolume;
import com.veeder.datacollection.rs232.response.TankResponseReport;
import com.veeder.datacollection.rs232.response.TankProfileAndFullVolume.Profile;

public class VRTLS2 extends VRTLS {

	/**
	 * get the probe offsets from the config file.
	 */
	@Override
	public Float getTankProbeOffsetFromATG(int tankId) {
		return Float.valueOf("0.0");
	}

	@Override
	public Float getTankCapacityFromATG(int tankId) {
		Float result = null;
		TankProfileAndFullVolume profileAndFullVolume= getAllTankProfileAndFullVolume(tankId);
		if(profileAndFullVolume!=null){
			result=Float.valueOf(profileAndFullVolume.getFullVol());
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private TankProfileAndFullVolume getAllTankProfileAndFullVolume(int tankId) {
		TankProfileAndFullVolume result = null;
		RS232Response response = getParsedResponse("iA30" + String.format("%02d", tankId),false);
		if (response instanceof TankResponseReport) {
			result = ((TankResponseReport<TankProfileAndFullVolume>) response).getResponse(tankId);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private Profile getTankProfile(int tankId) {
		Profile result = null;
		RS232Response response = getParsedResponse("iA30" + String.format("%02d", tankId),false);
		if (response instanceof TankResponseReport) {
			result = ((TankResponseReport<TankProfileAndFullVolume>) response).getResponse(tankId).getProfile();
		}
		return result;
	}

	@Override
	public TankChart getTankChartFromATG(int tankId) {
		Profile profile = getTankProfile(tankId);
		TankChart chart = null;
		if (profile == null) {
			return null;
		} else if (profile.equals(Profile.TwentyPoints)) {
			float diameter = getTankDiameterFromATG(tankId);
			Float[] volumes = getTank20PointsVolumesFromATG(tankId);
			if (volumes != null && volumes.length == 20) {
				chart = new TankChart();
				for (int i = 0; i < volumes.length; i++) {
					chart.addHVPair(new HeightVolumePair(diameter * (volumes.length - i)/volumes.length, volumes[i]));
				}
			}
		} else{
			chart = getMultipointTankChartFromATG(tankId);
		}

		return chart;
	}

	
	@SuppressWarnings("unchecked")
	private TankChart getMultipointTankChartFromATG(int tankId) {
		TankChart result = null;
		RS232Response response = getParsedResponse("iA31" + String.format("%02d", tankId),false);
		if (response instanceof TankResponseReport) {
			result = ((TankResponseReport<TankChart>) response).getResponse(tankId);
		}
		return result;
	}

	

}
